﻿namespace UnityEditor.Controls.InspectorControl
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.IO;
    using System.Linq.Expressions;
    using System.Windows;
    using System.Windows.Controls;
    using UnityEngine.Core;
    using UnityEditor.Controls.ComponentControl;
    using UnityEngine.IO;

    /// <summary>
    /// Interaction logic for FolderPicker.xaml
    /// </summary>
    public partial class InspectorControl : UserControl, INotifyPropertyChanged
    {
        private InspectorItem root;

        private string selectedPath = string.Empty;

        public InspectorControl()
        {
            this.InitializeComponent();

            this.Initialize();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public GameObject GameObject
        {
            get
            {
                return this.root.GameObject;
            }
            set
            {
                this.root.GameObject = value;
                this.Root = this.root;
            }
        }

        public InspectorItem Root
        {
            get
            {
                return this.root;
            }

            private set
            {
                // for some (unknown) reason this cannot be guarded with an if != value block ???
                this.root = value;
                if (value != null)
                    this.ParseGameObject(this.root.GameObject);
                this.NotifyPropertyChanged(() => this.Root);
            }
        }

        private void SavePrefab(object o, RoutedEventArgs e)
        {
            if (GameObject.HasPrefab)
            {
                IOManager.Instance.SaveGameObject(this.GameObject, this.GameObject.PrefabPath, false);
            }
        }

        private void ParseGameObject(GameObject gameObject)
        {
            if (GameObject.HasPrefab)
            {
                SavePrefabButton.IsEnabled = true;
            }
            else
            {
                SavePrefabButton.IsEnabled = false;
            }
            StackPanel.Children.Clear();
            foreach (UnityEngine.Core.Component component in gameObject.Components)
            {
                ComponentControl control = new ComponentControl();
                control.Component = component;
                StackPanel.Children.Add(control);
            }
        }

        #region INotifyPropertyChanged Members
        public void NotifyPropertyChanged<TProperty>(Expression<Func<TProperty>> property)
        {
            var lambda = (LambdaExpression)property;
            MemberExpression memberExpression;
            if (lambda.Body is UnaryExpression)
            {
                var unaryExpression = (UnaryExpression)lambda.Body;
                memberExpression = (MemberExpression)unaryExpression.Operand;
            }
            else
                memberExpression = (MemberExpression)lambda.Body;

            this.OnPropertyChanged(memberExpression.Member.Name);
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void Initialize()
        {
            DataContext = this.root;

            Workspace.Instance.GameObjectChanged += new EventHandler((o, e) =>
            {
                this.root = new InspectorItem(Workspace.Instance.GameObject);
                if (this.root.GameObject == null)
                    this.root = null;
                this.Root = this.root;
            });
        }
        #endregion
    }
}
